<template>
  <NavBar title="评论" backgroundColor="#161622">
    <template #left>
      <div class="left" @click="handleClick">
        <Icon name="zuojiantou" size="18" color="#fff"></Icon>
      </div>
    </template>
  </NavBar>
  <!-- 粉丝列表 -->
  <div class="fan_list">
    <div class="fan_item" v-for="item in commentList" :key="item.userId">
      <div class="avatar">

        <img :src="item.userAvatar" loading="lazy" alt="">
      </div>
      <div class="info">
        <div class="name">
          @{{ item.userNickname }}
        </div>
        <div class="desc">
          {{ item.commentContent }}
        </div>
        <div class="time">
          评论了你的评论
          {{ item.formattedTime }}
        </div>
      </div>
      <div class="follow">
        <img :src="item.videoCover" @error="item.videoCover = defaultvideoCover" loading="lazy" alt="">
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { getCommentListAPI } from '@/api/message'
import { useDateFormat } from '@/composables/useDateFormat'
import NavBar from '@/components/Common/NavBar/index.vue'
import Icon from '@/components/Common/Icon/index.vue'
import { useRouter } from 'vue-router'
import { useUserIDStore } from '@/stores/UserID'
const defaultvideoCover = 'https://img2.baidu.com/it/u=1436631956,3865584376&fm=253&app=138&f=JPEG?w=500&h=889'
// 路由
const router = useRouter()

// 用户 ID
const userIDStore = useUserIDStore()
const userId = userIDStore.userInfo?.userId

// 评论列表
const commentList = ref([])

// 返回消息页
const handleClick = () => {
  router.push({ name: 'message' })
}

// 获取评论列表
const getCommentList = async () => {
  try {
    const res = await getCommentListAPI(userId)
    res.data.forEach(item => {
      item.userAvatar = 'http://43.138.15.137:3000' + item.userAvatar
      item.formattedTime = useDateFormat(item.createdAt, 'YYYY-MM-DD', { relative: true }).formatted
    })
    commentList.value = res.data
  } catch (error) {
    console.error('获取评论列表失败:', error)
  }
}

// 挂载时获取数据
onMounted(() => {
  getCommentList()
})
</script>

<style lang="scss" scoped>
.fan_list {
  padding: 15px;
  background-color: #161622;


  .fan_item {
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    .avatar {
      width: 45px;
      height: 45px;
      border-radius: 50%;
      margin-right: 10px;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    .info {
      flex: 1;


      .name {
        font-size: 14px;
        font-weight: bold;
        line-height: 1;
        color: #fff;
      }

      .desc {
        font-size: 14px;
        color: #fff;
        margin-bottom: 5px;
        font-weight: bold;
      }

      .time {
        font-size: 12px;
        font-weight: bold;
        color: #879394;
      }
    }

    .follow {
      // width: 60px;
      // height: 24px;
      width: 60px;
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      font-weight: bold;
      color: #fff;
      border-radius: 4px;
      overflow: hidden;


      img {
        width: 100%;
        height: 100%;
        border-radius: 4px;

      }
    }
  }
}
</style>